package com.stone.databinding.data;

import androidx.databinding.Bindable;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;

import com.stone.databinding.util.ObservableViewModel;

/**
 * As an alternative to LiveData, you can use Observable Fields and binding properties.
 *
 * `Popularity` is exposed here as a `@Bindable` property so it's necessary to call
 * `notifyPropertyChanged` when any of the dependent properties change (`likes` in this case).
 */
public class ProfileObservableViewModel extends ObservableViewModel {
    private ObservableField<String> firstName = new ObservableField<>("Dan");
    private ObservableField<String> lastName = new ObservableField<>("Vie");
    private ObservableInt likes = new ObservableInt(0);

    public ObservableField<String> getFirstName() {
        return firstName;
    }

    public void setFirstName(ObservableField<String> firstName) {
        this.firstName = firstName;
    }

    public ObservableField<String> getLastName() {
        return lastName;
    }

    public void setLastName(ObservableField<String> lastName) {
        this.lastName = lastName;
    }

    public ObservableInt getLikes() {
        return likes;
    }

    public void setLikes(ObservableInt likes) {
        this.likes = likes;
    }

    public void onLike() {
        likes.set(likes.get() + 1);
        // You control when the @Bindable properties are updated using `notifyPropertyChanged()`.
        notifyPropertyChanged(com.stone.databinding.BR.popularity);
    }

    @Bindable
    public Popularity getPopularity() {
        int likesNum = likes.get();
        if (likesNum > 9) {
            return Popularity.STAR;
        } else if (likesNum > 4) {
            return Popularity.POPULAR;
        } else {
            return Popularity.NORMAL;
        }
    }
}
